<?php
namespace app\farm\model;

use app\BaseModel;
use app\farm\model\GoodsCate;
use app\shop\model\StoreGoods;
use think\facade\Db;

class LandList extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_land_list';

    protected $append = [

        'min_price'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-12 16:10
     */
    public function getCateIdAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'b.status'   => 1,

                'a.goods_id' => $data['id'],

                'a.type'     => 2,

                'b.type'     => 1,
            ];

            $goods_cate_model = new GoodsCate();

            $list = $goods_cate_model->alias('a')
                ->join('lbfarm_land_cate b','a.cate_id = b.id')
                ->where($dis)
                ->field('a.*,b.title')
                ->group('a.id')
                ->order('a.id dsec')
                ->column('b.id');


            return array_values($list);
        }

    }

//    /**
//     * @param $value
//     * @param $data
//     * @功能说明:
//     * @author chenniang
//     * @DataTime: 2022-01-26 15:16
//     */
//    public function getTextAttr($value,$data){
//
//        if(!empty($value)){
//
//
//            return str_replace("&amp;nbsp;","",getimgs($value));
//        }
//
//    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-07 10:50
     * @功能说明:轮播图转格式
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-31 17:46
     * @功能说明:获取最低价格
     */
    public function getMinPriceAttr($value,$data){

        if(!empty($data['id'])){

            $spe_model = new LandSpe();

            $price = $spe_model->where(['land_id'=>$data['id']])->min('price');

            return $price;
        }


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $g_data = $data;

        $arr = ['massif', 'seed', 'monitor','spe','cycle'];

        foreach ($arr as $vs){

            if(key_exists($vs,$data)){

                unset($data[$vs]);
            }

        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['cate_id'])){

            $cate_id = $data['cate_id'];

            unset($data['cate_id']);
        }

        $data['create_time'] = time();

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        $this->updateSome($id,$g_data,$cate_id,$data['uniacid']);


        return $id;

    }







    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $g_data = $data;

        $arr = ['massif', 'seed', 'monitor','spe','cycle'];

        foreach ($arr as $vs){

            if(key_exists($vs,$data)){

                unset($data[$vs]);
            }

        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['cate_id'])){

            $cate_id = $data['cate_id'];

            unset($data['cate_id']);
        }

        $res = $this->where($dis)->update($data);

        $this->updateSome($dis['id'],$g_data,$cate_id,$data['uniacid']);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 15:45
     * @功能说明:添加关联
     */
    public function updateSome($id,$data,$cate,$uniacid){

        $server = new \app\farm\server\Land();

        $lan_text_model = new LandText();

        $land_spe_model = new LandSpe();

        $server->addObserver($lan_text_model);

        $server->addObserver($land_spe_model);

        $server->notify($id,$data);

        $goods_cate_model = new GoodsCate();

        $goods_cate_model->addData($id,2,$uniacid,$cate);
        return true;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-20 14:00
     * @功能说明:前端土地列表
     */
    public function indexDataList($dis,$alh,$sort=1){

        switch ($sort){

            case 1:
                $order = 'id desc';

                break;

            case 3:

                $order = 'distance asc,id desc';

                break;

            case 2:

                $order = 'sale_num desc,id desc';

                break;

        }

        $data = $this->where($dis)->field(['*',$alh])->order($order)->paginate(10)->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-20 14:00
     * @功能说明:前端土地列表
     */
    public function indexDataListV2($dis,$alh,$sort=1){

        switch ($sort){

            case 1:
                $order = 'a.id desc';

                break;

            case 3:

                $order = 'distance asc,a.id desc';

                break;

            case 2:

                $order = 'a.sale_num desc,a.id desc';

                break;

        }

        $data = $this->alias('a')
                ->join('lbfarm_v2_goods_cate b','a.id = b.goods_id','left')
                ->where($dis)
                ->field(['a.*',$alh])
                ->group('a.id')
                ->order($order)
                ->paginate(10)
                ->toArray();

        return $data;


    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-15 14:24
     * @功能说明:土地详情
     */
    public function landInfo($dis){

        $data = $this->dataInfo($dis);

        $arr = [

            1 => [
                'type_name'=>'massif',

                'table'    => new Massif()

            ],
            2 => [

                'type_name'=> 'seed',

                'table'    => new Seed()

            ],
            4 => [
                'type_name'=> 'monitor',

                'table'    => new Monitor()

            ],
            5 => [

                'type_name'=>'cycle',

            ]
        ];

        $source_model = new Source();

        $land_text_model = new LandText();

        foreach ($arr as $k=>$v){

            if(empty($v['table'])){

                $where = [

                    'land_id' => $data['id'],

                    'type'    => $k,
                ];

                $obj = $land_text_model->where($where)->column('obj_id');

                $data[$v['type_name']] = array_values($obj);

                continue;
            }

            $where = [

                'b.land_id' => $data['id'],

                'b.type'    => $k,

                'a.status'  => 1
            ];

            $obj = $v['table']->alias('a')
                ->join('lbfarm_land_text b','b.obj_id = a.id','right')
                ->where($where)
                ->field('a.*,b.source_id as s_id')
                ->group('a.id')
                ->order('b.id desc')
                ->select()
                ->toArray();

            if(!empty($obj)){

                foreach ($obj as $key => &$value){

                    if(!empty($value['s_id'])){

                        $value['source_name'] = $source_model->where(['id'=>$value['s_id'],'status'=>1])->value('title');

                    }

                }

            }

            $data[$v['type_name']] = $obj;

        }

        $cate_model = new LandCate();

        $data['cate_name'] = $cate_model->where(['id'=>$data['cate_id'],'status'=>1,'type'=>1])->value('title');

        $land_spe    = new LandSpe();
        //规格
        $data['spe'] = $land_spe->where(['land_id'=>$data['id']])->select()->toArray();

        $mac_model = new Machine();

        $data['machine_name'] = $mac_model->where(['id'=>$data['machine_id'],'status'=>1])->value('title');

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-02 16:36
     * @功能说明:
     */
    public function landSomeFind($id,$type){

        $dis = [

            'obj_id' => $id,

            'type'   => $type,
        ];

        $data = $this->alias('a')
                ->join('lbfarm_land_text b','a.id = b.land_id')
                ->where($dis)
                ->where('a.status','>',-1)
                ->find();

        return !empty($data)?1:0;

    }








}